package com.totem.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.totem.admin.mapper.TSysCronMapper;
import com.totem.admin.model.TSysCron;
import com.totem.admin.service.ITSysCronService;
import com.totem.base.enums.TaskCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class TSysCronServiceImpl extends ServiceImpl<TSysCronMapper, TSysCron> implements ITSysCronService {

    @Override
    public boolean initData() {
        TSysCron fundReturnTask = TSysCron.builder()
                .taskName("基金收益任务")
                .taskCode(TaskCodeEnum.FUND_RETURN.getCode())
                .cron("0 1 0 * * *")
                .build();
        TSysCron fundAutoTask = TSysCron.builder()
                .taskName("基金自动复投任务")
                .taskCode(TaskCodeEnum.FUND_AUTO_INVEST.getCode())
                .cron("0 0 1 * * *")
                .build();
        TSysCron subscribeCancelTask = TSysCron.builder()
                .taskName("认购取消任务")
                .taskCode(TaskCodeEnum.SUBSCRIBE_CANCEL.getCode())
                .cron("0 0/5 * * * *")
                .build();
        TSysCron subscribeReturnTask = TSysCron.builder()
                .taskName("认购收益任务")
                .taskCode(TaskCodeEnum.SUBSCRIBE_RETURN.getCode())
                .cron("0 2 0 * * *")
                .build();
        TSysCron personalInvestTask = TSysCron.builder()
                .taskName("个人投入计算任务")
                .taskCode(TaskCodeEnum.PERSONAL_INVEST.getCode())
                .cron("0 0/15 * * * *")
                .build();
        TSysCron teamInvestTask = TSysCron.builder()
                .taskName("团队投入计算任务")
                .taskCode(TaskCodeEnum.TEAM_INVEST.getCode())
                .cron("0 0/5 * * * *")
                .build();
        TSysCron distributiveAwardTask = TSysCron.builder()
                .taskName("特殊认购奖励计算任务")
                .taskCode(TaskCodeEnum.SUBSCRIBE_AWARD.getCode())
                .cron("0 0/5 * * * *")
                .build();
        List<TSysCron> initData = List.of(fundReturnTask, fundAutoTask,
                subscribeCancelTask, subscribeReturnTask,
                personalInvestTask, teamInvestTask, distributiveAwardTask);
        LambdaQueryWrapper<TSysCron> initDataQuery = new LambdaQueryWrapper<>();
        Long count = baseMapper.selectCount(initDataQuery);
        if(count>=initData.size()){
            return true;
        }
        return super.saveBatch(initData);
    }
}
